home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pasnews.zip / SORTTEST.PAS < prev    next >
Pascal/Delphi Source File  |  1990-07-06  |  5KB  |  209 lines

  1. program SortTest;
  2.  
  3. {This program will compare 6 sorting algorithms:
  4.  Bubble Sort,
  5.  Selection Sort
  6.  Insertion Sort,
  7.  Shell Sort,
  8.  Merge Sort
  9.  and Quick Sort.
  10.  
  11.  The comparisons will include # of assignments, # of comparisons, and
  12.  total running time.
  13.  
  14. }
  15.  
  16. uses SortUnit, CRT, DOS;
  17.  
  18. const
  19.   MaxNumInts = 1000;
  20.  
  21.  
  22. var
  23.   Data,
  24.   Secondary  : DatArray;
  25.   StartClock : real;
  26.   NumItems   : integer;
  27.  
  28. function ClockOn : real;
  29.  
  30. var
  31.   hr,
  32.   min,
  33.   sec,
  34.   hs   : word;
  35.  
  36. begin
  37.   GetTime(hr, min, sec, hs);
  38.   ClockOn := hr*3600 + min*60 + sec + hs/100;
  39. end;
  40.  
  41. function ClockOff(StartVal : real) : real;
  42.  
  43. var
  44.   hr,
  45.   min,
  46.   sec,
  47.   hs   : word;
  48.   Temp : real;
  49.  
  50. begin
  51.   GetTime(hr, min, sec, hs);
  52.   ClockOff := hr*3600 + min*60 + sec + hs/100 - StartVal;
  53. end;
  54.  
  55.  
  56. procedure Build_List(Var table : DatArray; N : integer);
  57.  
  58. { Builds an array of N integers, from the random number generator
  59.   in Turbo Pascal. A small delay is added to insure that the
  60.   numbers are more random on faster machines. (20 MHZ +)           }
  61.  
  62. var
  63.   X: integer;
  64.  
  65. begin
  66.   randomize;
  67.   writeln('                            Building Table:');
  68.   for X:= 1 to N do
  69.     begin
  70.       { The delay here is to provide more random values for faster CPUs. }
  71.       delay(1);
  72.       gotoxy(45,1);
  73.       write(x:4);
  74.       Table[X] := random(1000);
  75.     end;
  76. end;
  77.  
  78. procedure Set_Up(var Table : DatArray; var NumItems : integer);
  79.  
  80. { Get the number of items the user wants to sort, and build
  81.   the table of random numbers...                              }
  82.  
  83. begin
  84.   clrscr;
  85.   gotoxy(20,12);
  86.   write('How many numbers to sort? ');
  87.   readln(NumItems);
  88.   clrscr;
  89.   Build_List(Table, NumItems);
  90. end;
  91.  
  92.  
  93. procedure DisplayTable(Table : DatArray; NumItems : integer);
  94.  
  95. { Display the un-sorted table for the user. }
  96.  
  97. var
  98.   X : integer;
  99.  
  100. begin
  101.   writeln;
  102.   writeln;
  103.   for X:= 1 to NumItems do
  104.     begin
  105.       write(Table[X]:6);
  106.       if X mod 12 = 0 then writeln;
  107.     end;
  108.     writeln;
  109. end;
  110.  
  111.  
  112.  
  113. begin
  114.   { Set everything up. }
  115.   Set_Up(Secondary, NumItems);
  116.  
  117.   { Display the table and wait for the user to continue.}
  118.   DisplayTable(Secondary, NumItems);
  119.   writeln;
  120.   writeln('                         Press Enter to Continue');
  121.   readln;
  122.  
  123.   { Ok, Time to start sorting. Set the screen up first. }
  124.   clrscr;
  125.   Data := Secondary;
  126.   writeln(' Sort Routine       Time      ');
  127.   writeln('---------------+--------------');
  128.  
  129.   { Ok, starting with the random order list, run each sort }
  130.   { ---> Bubble Sort }
  131.   StartClock := ClockOn;
  132.   Bubble_Sort(Data, NumItems);
  133.   writeln('Bubble         : ', ClockOff(StartClock):5:2, ' Secs.  |');
  134.   Data := Secondary;
  135.  
  136.   { ---> Selection Sort }
  137.   StartClock := ClockOn;
  138.   Select_Sort(Data, NumItems);
  139.   writeln('Selection      : ', ClockOff(StartClock):5:2, ' Secs.  |');
  140.   Data := Secondary;
  141.  
  142.   { ---> Insertion Sort }
  143.   StartClock := ClockOn;
  144.   Insert_Sort(Data, NumItems);
  145.   writeln('Insertion Sort : ', ClockOff(StartClock):5:2, ' Secs.  |');
  146.   Data := Secondary;
  147.  
  148.   { ---> Shell Sort }
  149.   StartClock := ClockOn;
  150.   Shell_Sort(Data, NumItems);
  151.   writeln('Shell Sort     : ', ClockOff(StartClock):5:2, ' Secs.  |');
  152.   Data := Secondary;
  153.  
  154.   { ---> Merge Sort }
  155.   StartClock := ClockOn;
  156.   Merge_Sort(Data, 1, NumItems);
  157.   writeln('Merge Sort     : ', ClockOff(StartClock):5:2, ' Secs.  |');
  158.   Data := Secondary;
  159.  
  160.  
  161.   { Because the QuickSort requires so much memory, it should be handled
  162.     seperately from all the other sorts.
  163.   }
  164.   { ---> Quick Sort }
  165. {  StartClock := ClockOn;
  166.   Quick_Sort(Data, 1, NumItems);
  167.   writeln('Quick Sort     : ', ClockOff(StartClock):5:2, ' Secs.  |');
  168. }
  169.   { Ok, now the list of ordered data! }
  170.   Writeln('-----------------> Pre-Sorted Data <---------------');
  171.  
  172.   { ---> Bubble Sort }
  173.   StartClock := ClockOn;
  174.   Bubble_Sort(Data, NumItems);
  175.   writeln('Bubble         : ', ClockOff(StartClock):5:2, ' Secs.  |');
  176.   Data := Secondary;
  177.  
  178.   { ---> Selection Sort }
  179.   StartClock := ClockOn;
  180.   Select_Sort(Data, NumItems);
  181.   writeln('Selection      : ', ClockOff(StartClock):5:2, ' Secs.  |');
  182.   Data := Secondary;
  183.  
  184.   { ---> Insertion Sort }
  185.   StartClock := ClockOn;
  186.   Insert_Sort(Data, NumItems);
  187.   writeln('Insertion Sort : ', ClockOff(StartClock):5:2, ' Secs.  |');
  188.   Data := Secondary;
  189.  
  190.   { ---> Shell Sort }
  191.   StartClock := ClockOn;
  192.   Shell_Sort(Data, NumItems);
  193.   writeln('Shell Sort     : ', ClockOff(StartClock):5:2, ' Secs.  |');
  194.   Data := Secondary;
  195.  
  196.   { ---> Merge Sort }
  197.   StartClock := ClockOn;
  198.   Merge_Sort(Data, 1, NumItems);
  199.   writeln('Merge Sort     : ', ClockOff(StartClock):5:2, ' Secs.  |');
  200.   Data := Secondary;
  201.  
  202.   { Because the QuickSort requires so much memory, it should be handled
  203.     seperately from all the other sorts.
  204.   }
  205.   { ---> Quick Sort }
  206. {  StartClock := ClockOn;
  207.   Quick_Sort(Data, 1, NumItems);
  208.   writeln('Quick Sort     : ', ClockOff(StartClock):5:2, ' Secs.  |'); }
  209. end.